package users

import (
	"github.com/revel/revel"
	//"revel_boot/app/controllers"
	"encoding/base64"
	"fmt"
	"net/http"
	//"time"
	//"strings"
)

type User struct {
	*revel.Controller
}

func (this *User) Login(pass string, user string) revel.Result {

	if this.Request.Method == "GET" || this.Request.Method == "get" {

		return this.Render()
	}

	return this.Render()
}

func (this *User) Index(pass string, user string) revel.Result {

	if this.Request.Method == "POST" || this.Request.Method == "POST" {

		cookie := new(http.Cookie)
		cookie.Name = "REVEL_BOOT"
		cookie.Path = "/"
		//cookie.Expires = time.Now().Add(time.Duration(int64(time.Hour) * 24 * 365))
		cookie.Value = base64.StdEncoding.EncodeToString([]byte("REVEL_BOOT_COOKIE"))
		this.SetCookie(cookie)

		this.Request.ParseForm()
		form := this.Request.Form

		user := form["user"][0]
		pass := form["pass"][0]

		resultJson := fmt.Sprintf(`{"Code":"%d","data":"%s"}`, 220, "success")
		revel.INFO.Println("user == ", user)
		revel.INFO.Println("pass == ", pass)
		revel.INFO.Println("resultJson == ", resultJson)

		return this.RenderText(resultJson)
	}
	if this.Request.Method == "GET" || this.Request.Method == "GET" {

		cookie, err := this.Request.Cookie("REVEL_BOOT") //如何获取cookie值--

		if err != nil || cookie.Value == "" {

			return this.Redirect("")
		}

		return this.Render()

	}

	return this.Render()
}
